package com.mujin.keji.collectionsystem.module.outboundtask.views;

import android.arch.lifecycle.Observer;
import android.arch.lifecycle.ViewModelProviders;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.mujin.keji.collectionsystem.R;
import com.mujin.keji.collectionsystem.base.BaseActivity;
import com.mujin.keji.collectionsystem.base.BaseDto;
import com.mujin.keji.collectionsystem.module.outboundtask.adapter.AssimilatorListAdapter;
import com.mujin.keji.collectionsystem.module.outboundtask.adapter.BoundTaskListAdapter;
import com.mujin.keji.collectionsystem.module.outboundtask.dto.ApplyCaseDto;
import com.mujin.keji.collectionsystem.module.outboundtask.dto.BoundTaskDto;
import com.mujin.keji.collectionsystem.module.outboundtask.viewmodel.BoundTaskListViewModel;
import com.mujin.keji.collectionsystem.module.outboundtask.vo.ApplyCaseVo;
import com.mujin.keji.collectionsystem.utils.DisplayUtil;
import com.mujin.keji.collectionsystem.utils.EmptyUtils;
import com.mujin.keji.collectionsystem.widget.CustomLinearLayoutManager;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

/**
 * 任务回收界面
 *
 * @author weishixiong
 * @Time 2018-05-21
 */

public class AssimilatorActivity extends BaseActivity implements BoundTaskListAdapter.BoundTaskListener {
    private CustomLinearLayoutManager linearLayoutManager;
    private List<BoundTaskDto> mBoundTaskDtoList;
    private BoundTaskListViewModel boundTaskListViewModel;
    private AssimilatorListAdapter adapter;
    private ApplyCaseVo applyCaseVo;
    private SmartRefreshLayout srHasData;
    private LinearLayout llNoData;
    private SmartRefreshLayout srNoData;
    private TextView tvNoData;
    private RecyclerView rvBaseTask;
    private int page = 1;
    private static final Integer PAGESIZE = 20;
    private TextView tvLeft;
    private TextView tvRight;
    private RelativeLayout rlLeft;
    private RelativeLayout rlRight;
    private int status;//4  超时回收。5 状态改变回收

    /**
     * 初始化请求任务回收列表的参数
     */
    private void initVo() {
        applyCaseVo = new ApplyCaseVo();
        applyCaseVo.setTimeSort(2);//时间降序
        applyCaseVo.setPageSize(PAGESIZE);
    }


    /**
     * 初始化拜访任务列表view
     */
    private void initTaskListView() {
        mBoundTaskDtoList = new ArrayList<>();
        linearLayoutManager = new CustomLinearLayoutManager(this);
        linearLayoutManager.setScrollEnabled(true);
        rvBaseTask.setLayoutManager(linearLayoutManager);
        adapter = new AssimilatorListAdapter(this, mBoundTaskDtoList);
        rvBaseTask.setAdapter(adapter);
    }

    /**
     * 请求外访任务列表的数据
     *
     * @param vo
     */
    private void loadData(ApplyCaseVo vo) {
        vo.setStatus(getStatus());
        vo.setCurrentPage(page);
        boundTaskListViewModel.getLiveData(vo).observe(this, new Observer<BaseDto<ApplyCaseDto>>() {
            @Override
            public void onChanged(@Nullable BaseDto<ApplyCaseDto> applyCaseDtoBaseDto) {
                srNoData.finishRefresh();
                srHasData.finishRefresh();
                srHasData.finishLoadMore();
                if (EmptyUtils.isNotEmpty(applyCaseDtoBaseDto.getData())
                        && EmptyUtils.isNotEmpty(applyCaseDtoBaseDto.getData().getItems())) {
                    llNoData.setVisibility(View.GONE);
                    srHasData.setVisibility(View.VISIBLE);
                    if (page == 1) {
                        mBoundTaskDtoList.clear();
                    }
                    mBoundTaskDtoList.addAll(applyCaseDtoBaseDto.getData().getItems());
                    adapter.setStatus(getStatus());
                    adapter.notifyDataSetChanged();
                } else {
                    if (page > 1) {
                        page--;
                    } else {
                        //第一次就没数据
                        mBoundTaskDtoList.clear();
                        adapter.notifyDataSetChanged();
                        llNoData.setVisibility(View.VISIBLE);
                        srHasData.setVisibility(View.GONE);
                        tvNoData.setText("暂无回收详情");
                    }
                }

            }
        });
    }

    private Integer getStatus() {
        return status;
    }

    @Override
    protected void createViewModel() {
        boundTaskListViewModel = ViewModelProviders.of(this).get(BoundTaskListViewModel.class);
    }

    @Override
    public int intiLayout() {
        return R.layout.activity_assimilator;
    }

    @Override
    public void initView() {
        setCenterTitle("任务回收详情", "#FFFFFF");
        setleftImage(R.drawable.image_arrow_left_white, true, null);
        srHasData = (SmartRefreshLayout) findViewById(R.id.sr_has_data);
        rvBaseTask = (RecyclerView) findViewById(R.id.rv_base_task);
        srNoData = (SmartRefreshLayout) findViewById(R.id.sr_no_data);
        llNoData = (LinearLayout) findViewById(R.id.ll_no_data);
        rvBaseTask = (RecyclerView) findViewById(R.id.rvBaseTask);
        tvNoData = (TextView) findViewById(R.id.tv_no_data);
        tvLeft = (TextView) findViewById(R.id.tv_left);
        tvRight = (TextView) findViewById(R.id.tv_right);
        rlLeft = (RelativeLayout) findViewById(R.id.rl_left);
        rlRight = (RelativeLayout) findViewById(R.id.rl_right);
        srNoData.setEnableLoadMore(false);
        srNoData.setHeaderHeight(DisplayUtil.dip2px(this,16));
        srHasData.setHeaderHeight(DisplayUtil.dip2px(this,16));
        initTaskListView();
    }

    @Override
    public void initData() {
        status = getIntent().getIntExtra("status", 4);
        if(4==status){
            rlLeft.setVisibility(View.VISIBLE);
            rlRight.setVisibility(View.INVISIBLE);
            tvLeft.setTextColor(Color.parseColor("#FF212128"));
            tvRight.setTextColor(Color.parseColor("#FFB2B2B2"));
        }
        else{
            rlLeft.setVisibility(View.INVISIBLE);
            rlRight.setVisibility(View.VISIBLE);
            tvLeft.setTextColor(Color.parseColor("#FFB2B2B2"));
            tvRight.setTextColor(Color.parseColor("#FF212128"));
        }
        initVo();
        srHasData.autoRefresh();
    }

    @Override
    public void initListener() {
        srHasData.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                page = 1;
                loadData(applyCaseVo);
            }
        });
        srHasData.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                page++;
                loadData(applyCaseVo);
            }
        });
        srNoData.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                page = 1;
                loadData(applyCaseVo);
            }
        });
        tvLeft.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                page = 1;
                status = 4;
                if (EmptyUtils.isNotEmpty(mBoundTaskDtoList)) {
                    srHasData.autoRefresh();
                } else {
                    srNoData.autoRefresh();
                }

                rlLeft.setVisibility(View.VISIBLE);
                rlRight.setVisibility(View.INVISIBLE);
                tvLeft.setTextColor(Color.parseColor("#FF212128"));
                tvRight.setTextColor(Color.parseColor("#FFB2B2B2"));
            }
        });
        tvRight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                page = 1;
                status = 5;
                if (EmptyUtils.isNotEmpty(mBoundTaskDtoList)) {
                    srHasData.autoRefresh();
                } else {
                    srNoData.autoRefresh();
                }
                rlLeft.setVisibility(View.INVISIBLE);
                rlRight.setVisibility(View.VISIBLE);
                tvLeft.setTextColor(Color.parseColor("#FFB2B2B2"));
                tvRight.setTextColor(Color.parseColor("#FF212128"));
            }
        });
    }

    @Override
    public void toCaseInformation(BoundTaskDto dto,int position) {

    }

    @Override
    public void toBoundDetail(BoundTaskDto dto,int position) {
    }
}
